package GA_Test;



import 单无人机架次.FullRandom;
import 多无人机架次.Compute;
import 多无人机架次.TimeCount;


import java.util.*;

/**
 * 个体类
 * 包含：
 * 1.createIndividual_greedy 初始物种基因(随机)
 * 2.calFitness 计算物种适应度
 * 3.printRate 打印路径
 */

public class SpeciesIndividual {

    ArrayList<Object> TruckGenes;//车基因序列组
    ArrayList<Object> DroneGenes;//无人机基因序列组
    public double individual_runtime;//
    double fitness;//适应度
    double selected_rate;

    public SpeciesIndividual() {
//初始化
        this.TruckGenes = new ArrayList<>();
        this.DroneGenes = new ArrayList<>();
        this.fitness = 0.0;
        this.individual_runtime = 0.0;  // 这里应该把distance 改成什么呢  改成时间？
        selected_rate = 0.0;

    }

    //初始物种基因(贪婪)
    void createIndividual_greedy() {
        Object[] hub = new Object[2];
        hub = Compute.mainMethod();
        //先让compute2返回一个同时包含 truck和drone 路径的数组   然后接住这个数组  把数组中的内容读出来  分别放到对应的位置
        TruckGenes = (ArrayList<Object>) hub[0];
        DroneGenes = (ArrayList<Object>) hub[1];//0 代表卡车的路径集   1代表 无人机的路径集

    }

    //计算物种适应度  适应度应该是计算什么的呢   应该是计算当前这个 个体的 运行时间分之一
     void calFitness() {
        double totalDis = 0;
        //整体思路 按车计算  这里 车对应的路线 必然是 无人机对应的路径
        for (int i = 0; i < this.TruckGenes.size(); i++) { //车辆循环

            ArrayList<Integer> single_truck = (ArrayList<Integer>) this.TruckGenes.get(i);
            ArrayList<Integer> single_drone = (ArrayList<Integer>) this.DroneGenes.get(i);
            totalDis += TimeCount.timeCount(single_truck, single_drone);
            //System.out.println(totalDis);
        }
        this.individual_runtime = totalDis;
        this.fitness = 1.0 / totalDis;
        //System.out.println(totalDis);
    }

//    // 复制一份 当前的个体
    public SpeciesIndividual clone() {
        SpeciesIndividual individual = new SpeciesIndividual();//把个体全部拿出来 重写一遍

        ArrayList<Integer> old_drone = new ArrayList<>();
        ArrayList<Integer> old_truck = new ArrayList<>();
        ArrayList<Object> drone_hub = new ArrayList<>();
        ArrayList<Object> truck_hub = new ArrayList<>();



        for (int i = 0; i < this.TruckGenes.size(); i++) {  //车辆信息复制
            old_truck = (ArrayList<Integer>) this.TruckGenes.get(i);
            ArrayList<Integer> truck_route = new ArrayList<>();
            for (int j = 0; j < old_truck.size(); j++) {
                truck_route.add(old_truck.get(j));
            }
            truck_hub.add(truck_route);
        }
        for (int i = 0; i < this.DroneGenes.size(); i++) { //无人机信息复制
            old_drone = (ArrayList<Integer>) this.DroneGenes.get(i);
            ArrayList<Integer> drone_route = new ArrayList<>();
            for (int j = 0; j < old_drone.size(); j++) {
                drone_route.add(old_drone.get(j));
            }
            drone_hub.add(drone_route);
        }
        individual.DroneGenes = drone_hub;
        individual.TruckGenes = truck_hub;
        individual.individual_runtime = this.individual_runtime;
        individual.fitness = this.fitness;
        individual.selected_rate = this.selected_rate;
        return individual;

    }
//
//    //打印路径
//    void printRate() {
//        System.out.print("最短路线：");
//        Print.print(this.TruckGenes,this.DroneGenes);
//        System.out.println("最短时间为" + 1/(this.fitness));
//    }
//
//}
}